/**
 * Created by billy on 2018/9/18.
 */
var AipSpeechClient = require("baidu-aip-sdk").speech;
var g = require("nodeLib");
// 设置APPID/AK/SK
var APP_ID = "9854110";
var API_KEY = "e808pvBGQWG0QYMKpypY0sDO";
var SECRET_KEY = "fYoowqXGnF8H8aKYXTNkdN1WkKoxjeD3";

// 新建一个对象，建议只保存一个对象调用服务接口
var client = new AipSpeechClient(APP_ID, API_KEY, SECRET_KEY);
var fs = require('fs');
var count = 14;
var param = {
	spd: 3,
	per: 0,
	pit: 6
}

//2 4 10  宝宝真棒

var list = ["请告诉我这个水果是大还是小? "];

module.exports = class {
	constructor()
	{
		this.add("start", this.run);
	}

	run($data, $succcess, $error, $client)
	{
		goNext();
	}
}

function goNext()
{
	if (list.length > 0)
	{
		if (list[0])
		{
			doVoice(list[0]);
		}
		else
		{
			end();
		}
	}
}

function end()
{
	list.shift();
	console.log(list.length);
	count++;
	goNext();
}

function doVoice($voice)
{
	client.text2audio($voice, param).then(function (result) {
		if (result.data)
		{
			g.fs.writeFileSync(`dv1_${count}.mp3`, result.data);
			end();
		}
		else
		{
			console.log(result)
		}
	}, function (e) {
		console.log(e)
	});
}